package localserver;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.concurrent.TimeUnit;

import common.AdministratorService;

public class AdministratorServiceImpl extends UnicastRemoteObject implements
		AdministratorService {

	private String geolocation;

	protected AdministratorServiceImpl(String geolocation)
			throws RemoteException {
		super();
		this.geolocation = geolocation;
	}

	public String getPlayerStatus(String adminUsername, String adminPassword,
			String iPAddress) throws RemoteException {
		String responseFromEU = null;
		String responseFromAS = null;
		String responseFromNA = null;

		common.Tools.writeLog("server", geolocation,
				"New admin call for getPlayerStatus at " + geolocation
						+ " server", true);

		if ((!adminUsername.equals("Admin"))
				|| (!adminPassword.equals("Admin"))) {
			common.Tools.writeLog("server", geolocation,
					"Wrong admin credentiels at " + geolocation + " server",
					true);
			throw new RemoteException("Wrong admin credentiels at "
					+ geolocation + " server");
		}

		if (geolocation.equals("EU")) {
			responseFromEU = UDPTools.getLocalUserStatus();

			responseFromNA = getUDPResult("NA", "EU");
			responseFromAS = getUDPResult("AS", "EU");

		} else if (geolocation.equals("AS")) {
			responseFromAS = UDPTools.getLocalUserStatus();
			responseFromNA = getUDPResult("NA", "AS");
			responseFromEU = getUDPResult("EU", "AS");

		} else if (geolocation.equals("NA")) {
			responseFromNA = UDPTools.getLocalUserStatus();
			responseFromEU = getUDPResult("EU", "NA");
			responseFromAS = getUDPResult("AS", "NA");
		}

		responseFromNA = (responseFromNA == null) ? "" : responseFromNA.trim();
		responseFromEU = (responseFromEU == null) ? "" : responseFromEU.trim();
		responseFromAS = (responseFromAS == null) ? "" : responseFromAS.trim();

		String response = "NA:" + responseFromNA + " EU:" + responseFromEU
				+ " AS:" + responseFromAS;

		common.Tools.writeLog("server", geolocation,
				"Response sent for  admin call for getPlayerStatus from "
						+ geolocation + " server Result:" + response, true);

		return response;
	}

	private String getUDPResult(String geolocation, String geolocationCaller) {
		UDPClient uDPClient = new UDPClient(common.Tools.getproperty(
				"server.properties", "serverName_" + geolocation),
				Integer.parseInt(common.Tools.getproperty("server.properties",
						"UDPPort_" + geolocation)), geolocation,
				geolocationCaller);

		Thread thread = new Thread(uDPClient);
		thread.start();
		int timeCounter = 0;

		while ((thread.isAlive()) && (timeCounter < 20)) {
			try {
				TimeUnit.MILLISECONDS.sleep(200);
				timeCounter++;
				common.Tools.writeLog("server", geolocation,
						"we are waiting for UDP response 2: " + geolocation
								+ " server", true);

			} catch (InterruptedException e) {
				common.Tools.writeLog("server", geolocation,
						"Exception while waiting for UDP response at : "
								+ geolocation + " server", true);

			}
		}

		return uDPClient.getResult();

	}

}